package ui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import ui.Main;
import util.Util;

/**
 * Action for file open.
 * @author TKington
 *
 */
public class OpenAction extends AbstractAction {
	/**
	 * Creates a new OpenAction.
	 */
	public OpenAction() {
		putValue(NAME, "Open");
		putValue(SHORT_DESCRIPTION, "Open File");
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		putValue(SMALL_ICON, Util.getIcon("Open16.gif")); //$NON-NLS-1$
	}
	
	public void actionPerformed(ActionEvent evt) {
        String filename = Util.chooseFile(Main.frame, Main.gbkFilter, "Open File",
                                    false, "Open");
        if(filename == null)
            return;

        try {
            Main.frame.onOpenFile(new File(filename));
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(Main.frame, "Error reading file.  See viewer.log");
            Util.logSilent(e);
        }
	}
}
